
<?php
/**
 * 分类管理模型
 */
class CategoryModel extends Model{
	public $table='category';
	public $auto=array(
		array('pid','getPid','method',2,1)
	);
	public function getPid(){
		return Q('get.pid',0,'intval');
	}
	public $validate=array(
		array('cname','nonull','分类名称不能为空',2,3)
	);
	
	/*添加分类
	 * */
	public function addCate(){
		if(!$this->create()) return false;
		return $this->add();
	}
	
	/*编辑分类
	 * */
	public function editCate(){
		if(!$this->create()) return false;
		$this->update();
		return true;
	}
	
	/*首页分类更改排序
	 * */
	public function toSort(){
		foreach (Q('post.') as $k => $s) {
			$this->where("cid={$k}")->update(array('sort'=>$s));
		}
	}
	
	/*获取不属于该分类子级的分类
	 * */
	public function getParentCate($cid){
		//找到给分类的子级
		$cateData=$this->field('cid,pid')->all();
		$sonCate=$this->getSonCate($cateData,$cid);
		$cateStr='';
		foreach ($sonCate as $c) {
			$cateStr.=$c['cid'].',';
		}
		$cateStr.=$cid;
		return $this->where("cid not in ($cateStr)")->all();
	}
	
	/*获取该分类的所有子级分类
	 * */
	public function getSonCate($data,$cid){
		$temp=array();
		foreach ($data as $c) {
			if($c['pid']==$cid){
				$temp[]=$c;
				$temp=array_merge($temp,$this->getSonCate($data,$c['cid']));
			}
		}
		return $temp;
	}
	/*获得当前分类的父级
	 **/
	public function getFather($data,$cid){
	    $temp=array();
		foreach ($data as $v) {
			if($v['cid']==$cid){
				$temp[]=$v;
				$temp=array_merge($temp,$this->getFather($data,$v['pid']));
			}
		}
		return $temp;
	}
	
	
	
	
	
	
	
	
}





    
    
    
?>